import React, { Component } from 'react'
import {NavBarBackItem, NavBarTitleItem, NavBarRightItem} from './NavbBarItems';
import NavigatorBarView from './NavigatorBarView';
import InteractionManager from 'InteractionManager';
import DCAction from '../actions/DCAction';
import dcalt from '../alt/DCAlt'
import DCStore from '../stores/DCStore'
import NotifyActions from '../actions/NotifyActions';
import LoadingFailView from '../config/LoadingFailView'
import HeadView from '../config/HeadView'
import TextView from '../config/Cell'

import {
	View,
	StyleSheet,
	ListView,
	TouchableOpacity,
	Text
}from 'react-native';

export default class UsersData extends Component {
	constructor(props) {
		super(props);
		this.state = {
			dataSource: [],
            numLunch: [],
            numSupper: [],
			showView: true,
		}
	}
	onChange = ()=> {
        this.setState({dataSource: ALT.stores.DCStore.getState().usersList})//用户
    };
    _renderRow =(rowData,sectionID,rowID)=> {
    	return (
    		<TouchableOpacity style = {{borderBottomColor:'rgb(230,230,230)',borderBottomWidth:1}}
    						  onPress = {() => this._pressRow(rowData)}>
                <View style = {{flexDirection: 'row'}}> 
                    <TextView text = {rowData}/>
                </View>
      		</TouchableOpacity>
    	);
    };
    _pressRow = (rowData)=> {
    	ALT.actions.DCAction.setRowID(rowData)
    	nav.push({title: 'DCData'})  	
    };
    //加载失败
    loadingFail = ()=> {
        this.state.showView = false
    };
	componentDidMount() {         
        InteractionManager.runAfterInteractions(()=>{
            ALT.actions.DCAction.queryUsers()
        });
        ALT.stores.DCStore.listen(this.onChange);
        NotifyActions.addNotifier('LoadingFailUsers', this.loadingFail, this);
    };
    componentWillUnmount() {
        ALT.stores.DCStore.unlisten(this.onChange);
        NotifyActions.removeNotifier('LoadingFailUsers', this.loadingFail, this);
    };


	render(){
		var content;


		if (this.state.showView == false) {
            content =  (<LoadingFailView/>);
        }else{
        	content = (<ListView 
						enableEmptySections = {true}
						dataSource={new ListView.DataSource({rowHasChanged: (r1,r2)=>r1 !== r2}).cloneWithRows(this.state.dataSource)}
      					renderRow={this._renderRow}/>);
        }


		return(
			<View style = {{width: Common.size.width,height: Common.size.height, flex: 1,backgroundColor: 'white'}}>
				<NavigatorBarView
						leftItem={<NavBarBackItem
						isTabBar={this.props.containerMarginBottom > 0? true: false}/>}
						centerItem ={<NavBarTitleItem
						title='用户列表'/>}/>	
				{content}			
			</View>
		)
	}
}
var styles = StyleSheet.create({
    contentText: {
        height: as(50),
        justifyContent: 'center',
        alignItems: 'center',
        flex: 1,
        borderWidth: 1,
        borderColor: 'rgb(230,230,230)',
    },
})

